local awful = require("awful")
local wibox = require("wibox")
local gears = require("gears")
local theme = require("theme")
local dpi	= require("beautiful.xresources").apply_dpi




local taskpanel = {}



taskpanel.widget = wibox.widget{
	{
		{
			{
				forced_width = dpi(1),
				widget = wibox.widget.textbox,
			},
			bg = theme.taskpanel_widget_line_bg,
			widget = wibox.container.background,
		},
		{
			{
				forced_width = dpi(5),
				widget = wibox.widget.textbox,
			},
			id = "background",
			bg = theme.taskpanel_widget_bg,
			widget = wibox.container.background,
		},
		layout = wibox.layout.fixed.horizontal,
	},
	widget	= wibox.container.margin,
	set_bg = function(self, var)
		self:get_children_by_id("background")[1].bg = var
	end,
	set_fg = function(self, var)
		self:get_children_by_id("background")[1].fg = var
	end
}




taskpanel.popup = awful.popup{
	widget = wibox.widget{
		{
			{
				{
					{
						text = "Workspace 1",
						font = theme.taskpanel_title_font,
						widget = wibox.widget.textbox,
					},
					fg = theme.taskpanel_title_fg,
					widget = wibox.container.background,
				},
				id = "title",
				margins = theme.taskpanel_title_margin,
				widget	= wibox.container.margin,
			},
			{
				id     = "workspace",
				layout = wibox.layout.fixed.vertical,
			},
			{
				{
					{
						text = "Workspace 2",
						font = theme.taskpanel_title_font,
						widget = wibox.widget.textbox,
					},
					fg = theme.taskpanel_title_fg,
					widget = wibox.container.background,
				},
				id = "title",
				visible = false,
				margins = theme.taskpanel_title_margin,
				widget	= wibox.container.margin,
			},
			{
				id     = "workspace",
				layout = wibox.layout.fixed.vertical,
			},
			{
				{
					{
						text = "Workspace 3",
						font = theme.taskpanel_title_font,
						widget = wibox.widget.textbox,
					},
					fg = theme.taskpanel_title_fg,
					widget = wibox.container.background,
				},
				id = "title",
				visible = false,
				margins = theme.taskpanel_title_margin,
				widget	= wibox.container.margin,
			},
			{
				id     = "workspace",
				layout = wibox.layout.fixed.vertical,
			},
			{
				{
					{
						text = "Workspace 4",
						font = theme.taskpanel_title_font,
						widget = wibox.widget.textbox,
					},
					fg = theme.taskpanel_title_fg,
					widget = wibox.container.background,
				},
				id = "title",
				visible = false,
				margins = theme.taskpanel_title_margin,
				widget	= wibox.container.margin,
			},
			{
				id     = "workspace",
				layout = wibox.layout.fixed.vertical,
			},
			{
				{
					{
						text = "Workspace 5",
						font = theme.taskpanel_title_font,
						widget = wibox.widget.textbox,
					},
					fg = theme.taskpanel_title_fg,
					widget = wibox.container.background,
				},
				id = "title",
				visible = false,
				margins = theme.taskpanel_title_margin,
				widget	= wibox.container.margin,
			},
			{
				id     = "workspace",
				layout = wibox.layout.fixed.vertical,
			},
			{
				{
					{
						text = "Workspace 6",
						font = theme.taskpanel_title_font,
						widget = wibox.widget.textbox,
					},
					fg = theme.taskpanel_title_fg,
					widget = wibox.container.background,
				},
				id = "title",
				visible = false,
				margins = theme.taskpanel_title_margin,
				widget	= wibox.container.margin,
			},
			{
				id     = "workspace",
				layout = wibox.layout.fixed.vertical,
			},
			{
				{
					{
						text = "Workspace 7",
						font = theme.taskpanel_title_font,
						widget = wibox.widget.textbox,
					},
					fg = theme.taskpanel_title_fg,
					widget = wibox.container.background,
				},
				id = "title",
				visible = false,
				margins = theme.taskpanel_title_margin,
				widget	= wibox.container.margin,
			},
			{
				id     = "workspace",
				layout = wibox.layout.fixed.vertical,
			},
			{
				{
					{
						text = "Workspace 8",
						font = theme.taskpanel_title_font,
						widget = wibox.widget.textbox,
					},
					fg = theme.taskpanel_title_fg,
					widget = wibox.container.background,
				},
				id = "title",
				visible = false,
				margins = theme.taskpanel_title_margin,
				widget	= wibox.container.margin,
			},
			{
				id     = "workspace",
				layout = wibox.layout.fixed.vertical,
			},
			{
				{
					{
						text = "Workspace 9",
						font = theme.taskpanel_title_font,
						widget = wibox.widget.textbox,
					},
					fg = theme.taskpanel_title_fg,
					widget = wibox.container.background,
				},
				id = "title",
				visible = false,
				margins = theme.taskpanel_title_margin,
				widget	= wibox.container.margin,
			},
			{
				id     = "workspace",
				layout = wibox.layout.fixed.vertical,
			},
			{
				{
					{
						text = "Workspace 10",
						font = theme.taskpanel_title_font,
						widget = wibox.widget.textbox,
					},
					fg = theme.taskpanel_title_fg,
					widget = wibox.container.background,
				},
				id = "title",
				visible = false,
				margins = theme.taskpanel_title_margin,
				widget	= wibox.container.margin,
			},
			{
				id     = "workspace",
				layout = wibox.layout.fixed.vertical,
			},
			layout = wibox.layout.fixed.vertical,
		},
		margins = theme.taskpanel_panel_margin,
		widget	= wibox.container.margin,
	},
	border_color	= theme.popup_border_color,
    border_width	= theme.popup_border_width,
	visible			= false,
	fg				= theme.taskpanel_fg,
	bg				= theme.taskpanel_bg,
	--minimum_height 	= theme.taskpanel_height,
	minimum_width   = theme.taskpanel_width,
	--maximum_height 	= theme.taskpanel_height,
	maximum_width   = theme.taskpanel_width,
	ontop			= true,
	shape			= function(cr, width, height)
		gears.shape.rounded_rect(cr, width, height, theme.popup_border_radius)
	end,
    placement		= function(wdg,args)  
		awful.placement.top_right(wdg, {margins = { top = theme.popup_margin_top ,right = theme.popup_margin_right}}) 
	end,
}



function taskpanel:task(c)
	if client.focus == c then
		list_fg = theme.taskpanel_list_curr_fg
		list_bg = theme.taskpanel_list_curr_bg
	else
		list_fg = theme.taskpanel_list_fg
		list_bg = theme.taskpanel_list_bg
	end

	local wdg = wibox.widget{
		{
			{
				{
					{
						{
							{
								forced_width  = dpi(3),
								forced_height = dpi(20),
								widget        = wibox.widget.textbox,
							},
							id     = "line",
							bg     = theme.taskpanel_list_line_bg,
							shape = function(cr, width, height)
								gears.shape.rounded_rect(cr, width, height, dpi(5))
							end,
							widget = wibox.container.background,
						},
						valign = "center",
						halign = "left",
						widget  = wibox.container.place,
					},
					left   = dpi(5),
					widget = wibox.container.margin,
				},
				{
					{
						{
							id     = "icon",
							valign = "center",
							halign = "center",
							widget = wibox.widget.imagebox,
						},
						forced_height = theme.taskpanel_icon_size,
						forced_width = theme.taskpanel_icon_size,
						valign = "center",
						halign = "center",
						widget	= wibox.container.place,
					},
					margins = theme.taskpanel_icon_margin,
					widget	= wibox.container.margin,
				},
				{
					{
						{
							id = "name",
							text = c.name,
							wrap = "char",
							line_spacing_factor = dpi(20),
							font = theme.taskpanel_list_font,
							widget = wibox.widget.textbox,
						},
						forced_height = theme.taskpanel_list_height,
						valign = "center",
						halign = "center",
						widget	= wibox.container.place,
					},
					margins = theme.taskpanel_name_margin,
					widget	= wibox.container.margin,
				},
				layout = wibox.layout.fixed.horizontal,
			},
			id = "background",
			fg = list_fg,
			bg = list_bg,
			shape = function(cr, width, height)
				gears.shape.rounded_rect(cr, width, height, theme.taskpanel_list_radius)
			end,
			widget = wibox.container.background,
		},
		margins = theme.taskpanel_list_margin,
		widget	= wibox.container.margin,
		buttons	= awful.util.table.join(
			awful.button({}, 1, nil, function()
				local current_tag = awful.screen.focused().selected_tag
				if c.first_tag ~= nil then
					if c.first_tag == current_tag then
						if client.focus == c then
							c.minimized = not c.minimized
						else
							c:jump_to()
						end
					else
						c:jump_to()
					end
				end
			end),
			awful.button({}, 2, nil, function()
				c:kill()
			end)
		),
		set_icon = function(self, icon)
			self:get_children_by_id("icon")[1].image = icon
		end,
		set_name = function(self, name)
			self:get_children_by_id("name")[1].text = name
		end,
		set_fg = function(self, var)
			self:get_children_by_id("background")[1].fg = var
		end,
		set_bg = function(self, var)
			self:get_children_by_id("background")[1].bg = var
		end,
		set_linebg = function(self, var)
			self:get_children_by_id("line")[1].bg = var
		end
	}
	wdg.tag = c.first_tag
	c.wdg = wdg
	return wdg
end



function taskpanel:add(c)
	local workspace = self.popup.widget:get_children_by_id("workspace")
	if workspace[c.first_tag.index] ~= nil then
		workspace[c.first_tag.index]:add(self:task(c))
	end
	if client.focus == c then
		c.wdg.linebg = theme.taskpanel_list_line_bg
	else
		c.wdg.linebg = ""
	end
	local cmd  = string.format("find %s -name %s.svg", theme.taskpanel_icon_path, c.instance or "application-default-icon")
	awful.spawn.easy_async_with_shell(cmd, function(out)
		if #out > 5 then
			c.wdg.icon = out:gsub("[\r\n]", "")
		else
			local cmd  = string.format("find %s -name %s.svg", theme.taskpanel_icon_path, c.class or "application-default-icon")
			awful.spawn.easy_async_with_shell(cmd, function(out)
				if #out > 5 then
					c.wdg.icon = out:gsub("[\r\n]", "")
				else
					c.wdg.icon = theme.taskpanel_default_icon
				end
			end)
		end
	end)
end



function taskpanel:remove(c)
	local workspace = self.popup.widget:get_children_by_id("workspace")
	if workspace[c.wdg.tag.index] ~= nil then
		workspace[c.wdg.tag.index]:remove_widgets(c.wdg)
	end
end


function taskpanel:toggle()
	self.popup.visible = not self.popup.visible
	if self.popup.visible then
		self.widget.bg = theme.taskpanel_widget_bg_hover
	else
		self.widget.bg = theme.taskpanel_widget_bg
	end
end


function taskpanel:setup(s)

	client.connect_signal("request::manage", function (c)
		if c.class == "QuakeDD" then
			return
		end
		taskpanel:add(c)
		c:connect_signal("property::tags", function (c)
			taskpanel:remove(c)
			taskpanel:add(c)
		end)
		c:connect_signal("property::name", function (c)
			c.wdg.name = c.name
		end)
		c:connect_signal("property::minimized", function (c)
			if c.minimized then	
				c.wdg.fg = theme.taskpanel_list_mini_fg
				c.wdg.bg = theme.taskpanel_list_mini_bg
				c.wdg.linebg = ""
			else
				c.wdg.fg = theme.taskpanel_list_fg
				c.wdg.bg = theme.taskpanel_list_bg
				c.wdg.linebg = ""
			end
		end)
		c:connect_signal("focus", function(c) 
			c.wdg.fg = theme.taskpanel_list_curr_fg
			c.wdg.bg = theme.taskpanel_list_curr_bg
			c.wdg.linebg = theme.taskpanel_list_line_bg
		end)
		c:connect_signal("unfocus", function(c) 
			c.wdg.fg = theme.taskpanel_list_fg
			c.wdg.bg = theme.taskpanel_list_bg
			c.wdg.linebg = ""
		end)
	end)

	client.connect_signal("request::unmanage", function (c)
		if c.class == "QuakeDD" then
			return
		end
		taskpanel:remove(c)
	end)

	--client.connect_signal("button::press", function (c)
	--	self.popup.visible = false
	--end)

	self.popup:buttons(gears.table.join(
		awful.button({ }, 3, function ()
			self.popup.visible = false
			self.widget.bg = theme.taskpanel_widget_bg
		end)
	))

	self.widget:buttons(awful.util.table.join (
		awful.button({}, 1, function() 
			self.popup.visible = not self.popup.visible
		end),
		awful.button({}, 3, function() 
			self.popup.visible = not self.popup.visible
		end)
	))

	self.widget:connect_signal('mouse::enter',function() 
		self.widget.bg = theme.taskpanel_widget_bg_hover
		c = client.focus
		if c ~= nil and c.class ~= "QuakeDD" then
			c.wdg.fg = theme.taskpanel_list_curr_fg
			c.wdg.bg = theme.taskpanel_list_curr_bg
		end
		--self.popup.visible = true
	end)

	self.widget:connect_signal('mouse::leave',function() 
		if self.popup.visible then
			self.widget.bg = theme.taskpanel_widget_bg_hover
		else
			self.widget.bg = theme.taskpanel_widget_bg
		end
	end)

	return self.widget
end




return taskpanel
